//
//  MapViewController.m
//  Frame
//
//  Created by 这是工作本 on 16/1/18.
//  Copyright © 2016年 这是工作本. All rights reserved.
//

#import "BMKPointAnnotation.h"
#import "BMKMapView.h"
#import "BMKRouteSearch.h"
#import "BMKLocationService.h"
#import "UIImage+Rotate.h"
#import "BMKPolyline.h"
#import "BMKPolylineView.h"
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>




@interface MapViewController ()<UIGestureRecognizerDelegate, BMKMapViewDelegate, BMKRouteSearchDelegate, BMKLocationServiceDelegate>
@property (nonatomic, retain) BMKMapView *mapView;

@property (nonatomic, retain) BMKRouteSearch *routesearch;

@property (nonatomic, retain) UITextField *startLoction;

@property (nonatomic, retain) UITextField *endLoction;

@property (nonatomic, retain) UIButton *bus;

@property (nonatomic, retain) UIButton *drive;

@property (nonatomic, retain) UIButton *walk;

@property (nonatomic, retain) BMKLocationService *locService;

@property (nonatomic, retain) UIButton *button;

@end

@interface RouteAnnotation : BMKPointAnnotation
{
    int _type; ///<0:起点 1：终点 2：公交 3：地铁 4:驾乘 5:途经点
    int _degree;
}

@property (nonatomic) int type;
@property (nonatomic) int degree;
@end

@implementation RouteAnnotation

@synthesize type = _type;
@synthesize degree = _degree;
@end



@implementation MapViewController

-(void)viewWillAppear:(BOOL)animated {
    [_mapView viewWillAppear];
    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
    _routesearch.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
    self.navigationController.navigationBar.translucent = YES;
    self.tabBarController.tabBar.hidden = YES;
    
    
}


- (void)viewDidAppear:(BOOL)animated
{
    self.bus.hidden = NO;
    self.walk.hidden = NO;
    self.drive.hidden = NO;
    self.button.hidden = NO;
    self.startLoction.hidden = NO;

}

-(void)viewWillDisappear:(BOOL)animated {
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时，置nil
    _routesearch.delegate = nil; // 不用时，置nil
    
    self.bus.hidden = YES;
    self.walk.hidden = YES;
    self.drive.hidden = YES;
    self.button.hidden = YES;
    self.startLoction.hidden = YES;
    
    
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    //隐藏自动返回
    self.navigationItem.hidesBackButton = YES;
    //返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"矩形-3@2x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    
    
    self.mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64)];
    [self.view addSubview:_mapView];
    
//    UILabel *start = [[UILabel alloc] initWithFrame:CGRectMake(10 * ScreenWidth, 84 * ScreenHeigth,  100 * ScreenWidth, 30 * ScreenHeigth)];
//    start.text = @"起点";
//    [self.view addSubview:start];
    
//    self.startLoction = [[UITextField alloc] initWithFrame:CGRectMake(start.frame.origin.x + start.frame.size.width, start.frame.origin.y, [UIScreen mainScreen].bounds.size.width - start.frame.size.width + start.frame.origin.x, start.frame.size.height)];
////    self.startLoction.placeholder = @"请输入起始位置";
//    [self.navigationController.navigationBar addSubview:self.startLoction];
    
    
//    UILabel *end = [[UILabel alloc] initWithFrame:CGRectMake(start.frame.origin.x, start.frame.origin.y + start.frame.size.height + 10 * ScreenHeigth, start.frame.size.width, start.frame.size.height)];
////    end.text = @"终点";
//    [self.view addSubview:end];
    
    
//    self.endLoction = [[UITextField alloc] initWithFrame:CGRectMake(end.frame.origin.x + end.frame.size.width, end.frame.origin.y, [UIScreen mainScreen].bounds.size.width - end.frame.size.width + end.frame.origin.x, end.frame.size.height)];
//    
//    self.endLoction.placeholder = @"请输入目的位置";
//    [self.view addSubview:self.endLoction];
    
    
    self.bus = [UIButton buttonWithType:UIButtonTypeCustom];
    self.bus.frame = CGRectMake(70 * ScreenWidth, 10 * ScreenHeigth, 30 * ScreenWidth, 30 * ScreenHeigth);
//    [self.bus setTitle:@"公交" forState:UIControlStateNormal];
    [self.bus setImage:[UIImage imageNamed:@"iconfont-gongjiao.png"] forState:UIControlStateNormal];
//    self.bus.backgroundColor = [UIColor redColor];
    [self.bus addTarget:self action:@selector(onClickBusSearch) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationController.navigationBar addSubview:self.bus];
    
    
    self.drive = [UIButton buttonWithType:UIButtonTypeCustom];
    self.drive.frame = CGRectMake(self.bus.frame.origin.x + self.bus.frame.size.width + 10 * ScreenWidth, self.bus.frame.origin.y, 30 * ScreenWidth, 30 * ScreenHeigth);
//    [self.drive setTitle:@"驾车" forState:UIControlStateNormal];
    [self.drive setImage:[UIImage imageNamed:@"iconfont-jiache.png"] forState:UIControlStateNormal];
//    self.drive.backgroundColor = [UIColor redColor];
    [self.drive addTarget:self action:@selector(onClickDriveSearch) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationController.navigationBar addSubview:self.drive];
    
    self.walk = [UIButton buttonWithType:UIButtonTypeCustom];
    self.walk.frame = CGRectMake(self.drive.frame.origin.x + self.drive.frame.size.width + 10 * ScreenWidth, self.bus.frame.origin.y , 30 * ScreenWidth, 30 * ScreenHeigth);
//    [self.walk setTitle:@"步行" forState:UIControlStateNormal];
    [self.walk setImage:[UIImage imageNamed:@"iconfont-jiaotongiconwalk.png"] forState:UIControlStateNormal];

//    self.walk.backgroundColor = [UIColor redColor];
    [self.walk addTarget:self action:@selector(onClickWalkSearch) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationController.navigationBar addSubview:self.walk];
    

    
    //创建定位对象
    
    self.locService = [[BMKLocationService alloc] init];
    self.locService.delegate = self;
    //    [BMKLocationService setLocationDistanceFilter:10];
    self.locService.distanceFilter = 10 * ScreenWidth;
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(self.walk.frame.origin.x + self.walk.frame.size.width + 20 * ScreenWidth, self.bus.frame.origin.y, 30 * ScreenWidth, 30 * ScreenHeigth);
//    self.button.backgroundColor = [UIColor orangeColor];
    [self.button setImage:[UIImage imageNamed:@"iconfont-dingwei.png"] forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationController.navigationBar addSubview:self.button];
    
    
    
    self.startLoction = [[UITextField alloc] initWithFrame:CGRectMake(self.button.frame.origin.x + self.button.frame.size.width + 10 * ScreenWidth, self.bus.frame.origin.y, 160 * ScreenWidth, 30)];
    self.startLoction.placeholder = @"请输入起始位置";
    self.startLoction.font = [UIFont systemFontOfSize:13.0];
    self.startLoction.textColor = [UIColor orangeColor];
    self.startLoction.text = @"丽都饭店";
    self.startLoction.textColor = [UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:230 / 255.0 alpha:1.0];
    [self.navigationController.navigationBar addSubview:self.startLoction];

    
    
    //适配ios7
    if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0))
    {
        //        self.edgesForExtendedLayout=UIRectEdgeNone;
        self.navigationController.navigationBar.translucent = NO;
        
    }
    
    _routesearch = [[BMKRouteSearch alloc]init];
//    _startLoction.text = @"丽都饭店";
//    _endLoction.text = @"金台路";

    
    
    
}


- (void)buttonAction
{
    [self.locService startUserLocationService];
    self.mapView.showsUserLocation = YES;
    
}

- (void)willStartLocatingUser
{
    NSLog(@"开始定位");
}


- (void)didFailToLocateUserWithError:(NSError *)error
{
    
    NSLog(@"定位失败:%@", error);
}

//定位成功再次定位

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    //定义大头针标注
    BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc] init];
    
    //设置大头针坐标
    annotation.coordinate = userLocation.location.coordinate;
    
    
    //定位获得经度和纬度
//    self.latitude = userLocation.location.coordinate.latitude;
//    self.longtitude = userLocation.location.coordinate.longitude;
//

    [self.locationDelegate latitude:userLocation.location.coordinate.latitude longtitude:userLocation.location.coordinate.longitude];
    
    //NSLog(@"%@----", userLocation.location);
    
    annotation.title = @"北京市朝阳区丽都酒店";
    //添加到地图中
    [self.mapView addAnnotation:annotation];
    
    //使用地图显示该位置
    
    [self.mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
    
    
}

- (void)applicationWillResignActive:(UIApplication *)application {
    [BMKMapView willBackGround];//当应用即将后台时调用，停止一切调用opengl相关的操作
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
    [BMKMapView didForeGround];//当应用恢复前台状态时调用，回复地图的渲染和opengl相关的操作
}



- (BMKAnnotationView*)getRouteAnnotationView:(BMKMapView *)mapview viewForAnnotation:(RouteAnnotation*)routeAnnotation
{
    BMKAnnotationView* view = nil;
    switch (routeAnnotation.type) {
        case 0:
        {
            view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"start_node"];
            if (view == nil) {
                view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"start_node"];
                //                view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_start.png"]];
                view.image = [UIImage imageNamed:@"icon_nav_start.png"];
                view.centerOffset = CGPointMake(0, -(view.frame.size.height * 0.5));
                view.canShowCallout = TRUE;
            }
            view.annotation = routeAnnotation;
        }
            break;
        case 1:
        {
            view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"end_node"];
            if (view == nil) {
                view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"end_node"];
                //                view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_end.png"]];
                view.image = [UIImage imageNamed:@"icon_nav_end.png"];
                view.centerOffset = CGPointMake(0, -(view.frame.size.height * 0.5));
                view.canShowCallout = TRUE;
            }
            view.annotation = routeAnnotation;
        }
            break;
        case 2:
        {
            view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"bus_node"];
            if (view == nil) {
                view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"bus_node"];
                //                view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_bus.png"]];
                view.image = [UIImage imageNamed:@"icon_nav_bus.png"];
                view.canShowCallout = TRUE;
            }
            view.annotation = routeAnnotation;
        }
            break;
        case 3:
        {
            view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"rail_node"];
            if (view == nil) {
                view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"rail_node"];
                //                view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_rail.png"]];
                view.image = [UIImage imageNamed:@"icon_nav_rail.png"];
                view.canShowCallout = TRUE;
            }
            view.annotation = routeAnnotation;
        }
            break;
        case 4:
        {
            view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"route_node"];
            if (view == nil) {
                view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"route_node"];
                view.canShowCallout = TRUE;
            } else {
                [view setNeedsDisplay];
            }
            
            //            UIImage* image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_direction.png"]];
            UIImage *image = [UIImage imageNamed:@"icon_direction.png"];
            view.image = [image imageRotatedByDegrees:routeAnnotation.degree];
            view.annotation = routeAnnotation;
            
        }
            break;
        case 5:
        {
            view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"waypoint_node"];
            if (view == nil) {
                view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"waypoint_node"];
                view.canShowCallout = TRUE;
            } else {
                [view setNeedsDisplay];
            }
            //
            //            UIImage* image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_waypoint.png"]];
            UIImage *image = [UIImage imageNamed:@"icon_nav_waypoint.png"];
            view.image = [image imageRotatedByDegrees:routeAnnotation.degree];
            view.annotation = routeAnnotation;
        }
            break;
        default:
            break;
    }
    
    return view;
}

- (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[RouteAnnotation class]]) {
        return [self getRouteAnnotationView:view viewForAnnotation:(RouteAnnotation*)annotation];
    }
    return nil;
}

- (BMKOverlayView*)mapView:(BMKMapView *)map viewForOverlay:(id<BMKOverlay>)overlay
{
    if ([overlay isKindOfClass:[BMKPolyline class]]) {
        BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
        polylineView.fillColor = [[UIColor alloc] initWithRed:0 green:1 blue:1 alpha:1];
        polylineView.strokeColor = [[UIColor alloc] initWithRed:0 green:0 blue:1 alpha:0.7];
        polylineView.lineWidth = 3.0;
        return polylineView;
    }
    return nil;
}

- (void)onGetTransitRouteResult:(BMKRouteSearch*)searcher result:(BMKTransitRouteResult*)result errorCode:(BMKSearchErrorCode)error
{
    NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
    [_mapView removeAnnotations:array];
    array = [NSArray arrayWithArray:_mapView.overlays];
    [_mapView removeOverlays:array];
    if (error == BMK_SEARCH_NO_ERROR) {
        BMKTransitRouteLine* plan = (BMKTransitRouteLine*)[result.routes objectAtIndex:0];
        // 计算路线方案中的路段数目
        NSInteger size = [plan.steps count];
        int planPointCounts = 0;
        for (int i = 0; i < size; i++) {
            BMKTransitStep* transitStep = [plan.steps objectAtIndex:i];
            if(i==0){
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item.coordinate = plan.starting.location;
                item.title = @"起点";
                item.type = 0;
                [_mapView addAnnotation:item]; // 添加起点标注
                
            }else if(i==size-1){
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item.coordinate = plan.terminal.location;
                item.title = @"终点";
                item.type = 1;
                [_mapView addAnnotation:item]; // 添加起点标注
            }
            RouteAnnotation* item = [[RouteAnnotation alloc]init];
            item.coordinate = transitStep.entrace.location;
            item.title = transitStep.instruction;
            item.type = 3;
            [_mapView addAnnotation:item];
            
            //轨迹点总数累计
            planPointCounts += transitStep.pointsCount;
        }
        
        //轨迹点
        
        BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];
        int i = 0;
        for (int j = 0; j < size; j++) {
            BMKTransitStep* transitStep = [plan.steps objectAtIndex:j];
            int k=0;
            for(k=0;k<transitStep.pointsCount;k++) {
                temppoints[i].x = transitStep.points[k].x;
                temppoints[i].y = transitStep.points[k].y;
                i++;
            }
            
        }
        // 通过points构建BMKPolyline
        BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
        [_mapView addOverlay:polyLine]; // 添加路线overlay
        delete []temppoints;
        [self mapViewFitPolyLine:polyLine];
    }
}
- (void)onGetDrivingRouteResult:(BMKRouteSearch*)searcher result:(BMKDrivingRouteResult*)result errorCode:(BMKSearchErrorCode)error
{
    NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
    [_mapView removeAnnotations:array];
    array = [NSArray arrayWithArray:_mapView.overlays];
    [_mapView removeOverlays:array];
    if (error == BMK_SEARCH_NO_ERROR) {
        BMKDrivingRouteLine* plan = (BMKDrivingRouteLine*)[result.routes objectAtIndex:0];
        // 计算路线方案中的路段数目
        NSInteger size = [plan.steps count];
        int planPointCounts = 0;
        for (int i = 0; i < size; i++) {
            BMKDrivingStep* transitStep = [plan.steps objectAtIndex:i];
            if(i==0){
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item.coordinate = plan.starting.location;
                item.title = @"起点";
                item.type = 0;
                [_mapView addAnnotation:item]; // 添加起点标注
                
            }else if(i==size-1){
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item.coordinate = plan.terminal.location;
                item.title = @"终点";
                item.type = 1;
                [_mapView addAnnotation:item]; // 添加起点标注
            }
            //添加annotation节点
            RouteAnnotation* item = [[RouteAnnotation alloc]init];
            item.coordinate = transitStep.entrace.location;
            item.title = transitStep.entraceInstruction;
            item.degree = transitStep.direction * 30;
            item.type = 4;
            [_mapView addAnnotation:item];
            
            //轨迹点总数累计
            planPointCounts += transitStep.pointsCount;
        }
        // 添加途经点
        if (plan.wayPoints) {
            for (BMKPlanNode* tempNode in plan.wayPoints) {
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item = [[RouteAnnotation alloc]init];
                item.coordinate = tempNode.pt;
                item.type = 5;
                item.title = tempNode.name;
                [_mapView addAnnotation:item];
            }
        }
        //轨迹点
        BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];
        
        int i = 0;
        for (int j = 0; j < size; j++) {
            BMKDrivingStep* transitStep = [plan.steps objectAtIndex:j];
            int k=0;
            for(k=0;k<transitStep.pointsCount;k++) {
                temppoints[i].x = transitStep.points[k].x;
                temppoints[i].y = transitStep.points[k].y;
                i++;
            }
            
        }
        // 通过points构建BMKPolyline
        BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
        [_mapView addOverlay:polyLine]; // 添加路线overlay
        delete []temppoints;
        [self mapViewFitPolyLine:polyLine];
    }
}

- (void)onGetWalkingRouteResult:(BMKRouteSearch*)searcher result:(BMKWalkingRouteResult*)result errorCode:(BMKSearchErrorCode)error
{
    NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
    [_mapView removeAnnotations:array];
    array = [NSArray arrayWithArray:_mapView.overlays];
    [_mapView removeOverlays:array];
    if (error == BMK_SEARCH_NO_ERROR) {
        BMKWalkingRouteLine* plan = (BMKWalkingRouteLine*)[result.routes objectAtIndex:0];
        NSInteger size = [plan.steps count];
        int planPointCounts = 0;
        for (int i = 0; i < size; i++) {
            BMKWalkingStep* transitStep = [plan.steps objectAtIndex:i];
            if(i==0){
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item.coordinate = plan.starting.location;
                item.title = @"起点";
                item.type = 0;
                [_mapView addAnnotation:item]; // 添加起点标注
                
            }else if(i==size-1){
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item.coordinate = plan.terminal.location;
                item.title = @"终点";
                item.type = 1;
                [_mapView addAnnotation:item]; // 添加起点标注
            }
            //添加annotation节点
            RouteAnnotation* item = [[RouteAnnotation alloc]init];
            item.coordinate = transitStep.entrace.location;
            item.title = transitStep.entraceInstruction;
            item.degree = transitStep.direction * 30;
            item.type = 4;
            [_mapView addAnnotation:item];
            
            //轨迹点总数累计
            planPointCounts += transitStep.pointsCount;
        }
        
        //轨迹点
        BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];
        int i = 0;
        for (int j = 0; j < size; j++) {
            BMKWalkingStep* transitStep = [plan.steps objectAtIndex:j];
            int k=0;
            for(k=0;k<transitStep.pointsCount;k++) {
                temppoints[i].x = transitStep.points[k].x;
                temppoints[i].y = transitStep.points[k].y;
                i++;
            }
            
        }
        // 通过points构建BMKPolyline
        BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
        [_mapView addOverlay:polyLine]; // 添加路线overlay
        delete []temppoints;
        [self mapViewFitPolyLine:polyLine];
    }
}

-(IBAction)onClickBusSearch
{
    BMKPlanNode* start = [[BMKPlanNode alloc]init];
    start.name = _startLoction.text;
    BMKPlanNode* end = [[BMKPlanNode alloc]init];
//    end.name = _endLoction.text;
      end.name = @"金台路";
    
    BMKTransitRoutePlanOption *transitRouteSearchOption = [[BMKTransitRoutePlanOption alloc]init];
    transitRouteSearchOption.city= @"北京市";
    transitRouteSearchOption.from = start;
    transitRouteSearchOption.to = end;
    BOOL flag = [_routesearch transitSearch:transitRouteSearchOption];
    
    if(flag)
    {
        NSLog(@"bus检索发送成功");
    }
    else
    {
        NSLog(@"bus检索发送失败");
    }
}

-(IBAction)textFiledReturnEditing:(id)sender {
    [sender resignFirstResponder];
}

-(IBAction)onClickDriveSearch
{
    BMKPlanNode* start = [[BMKPlanNode alloc]init];
    start.name = _startLoction.text;
    start.cityName = @"北京市";
    BMKPlanNode* end = [[BMKPlanNode alloc]init];
//    end.name = _endLoction.text;
    end.name = @"金台路";
    end.cityName = @"北京市";
    
    BMKDrivingRoutePlanOption *drivingRouteSearchOption = [[BMKDrivingRoutePlanOption alloc]init];
    drivingRouteSearchOption.from = start;
    drivingRouteSearchOption.to = end;
    BOOL flag = [_routesearch drivingSearch:drivingRouteSearchOption];
    if(flag)
    {
        NSLog(@"car检索发送成功");
    }
    else
    {
        NSLog(@"car检索发送失败");
    }
    
}

-(IBAction)onClickWalkSearch
{
    
    BMKPlanNode* start = [[BMKPlanNode alloc]init];
    start.name = _startLoction.text;
    start.cityName = @"北京市";
    BMKPlanNode* end = [[BMKPlanNode alloc]init];
    end.name = _endLoction.text;
      end.name = @"金台路";
//    end.cityName = @"北京市";
    
    
    BMKWalkingRoutePlanOption *walkingRouteSearchOption = [[BMKWalkingRoutePlanOption alloc]init];
    walkingRouteSearchOption.from = start;
    walkingRouteSearchOption.to = end;
    BOOL flag = [_routesearch walkingSearch:walkingRouteSearchOption];
    if(flag)
    {
        NSLog(@"walk检索发送成功");
    }
    else
    {
        NSLog(@"walk检索发送失败");
    }
    
}

//根据polyline设置地图范围
- (void)mapViewFitPolyLine:(BMKPolyline *) polyLine {
    CGFloat ltX, ltY, rbX, rbY;
    if (polyLine.pointCount < 1) {
        return;
    }
    BMKMapPoint pt = polyLine.points[0];
    ltX = pt.x, ltY = pt.y;
    rbX = pt.x, rbY = pt.y;
    for (int i = 1; i < polyLine.pointCount; i++) {
        BMKMapPoint pt = polyLine.points[i];
        if (pt.x < ltX) {
            ltX = pt.x;
        }
        if (pt.x > rbX) {
            rbX = pt.x;
        }
        if (pt.y > ltY) {
            ltY = pt.y;
        }
        if (pt.y < rbY) {
            rbY = pt.y;
        }
    }
    BMKMapRect rect;
    rect.origin = BMKMapPointMake(ltX , ltY);
    rect.size = BMKMapSizeMake(rbX - ltX, rbY - ltY);
    
    [_mapView setVisibleMapRect:rect];
    _mapView.zoomLevel = _mapView.zoomLevel - 0.3;
}


- (void)leftBarButtonAction:(UIBarButtonItem *)sender
{
        
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
